package common.dao.Impl;

import cn.hutool.core.util.StrUtil;
import common.dao.TeacherCourseDao;
import common.domain.TeacherCourse;
import utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Date 2023/1/11 10:38
 * @Author lihu
 */
public class TeacherCourseDaoImpl implements TeacherCourseDao {
    private Connection con;
    private PreparedStatement psmt;
    private ResultSet rs;
    @Override
    public void insert(TeacherCourse teacherCourse) {
        String sql = "insert into teacher_course\n" +
                "        (teacher_id, course_id) \n" +
                "values(?,?)";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1, teacherCourse.getTeacherId());
            psmt.setLong(2, teacherCourse.getCourseId());
            psmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt);
        }
    }

    @Override
    public void updateById(TeacherCourse teacherCourse) {
        String sql = "update teacher_course set";
        List<Object> param = new ArrayList<>();
        if (teacherCourse == null) {
            return;
        } else {
            if (teacherCourse.getTeacherId() != null) {
                sql += " teacher_id = ?,";
                param.add(teacherCourse.getCourseId());
            }
            if (teacherCourse.getCourseId() != null) {
                sql += " course_id= ?,";
                param.add(teacherCourse.getCourseId());
            }

        }
        sql = StrUtil.strip(sql, null, ",");
        sql += " where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                psmt.setObject(i + 1, param.get(i));
            }
            psmt.setLong(param.size() + 1, teacherCourse.getId());
            psmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt);
        }
    }

    @Override
    public void deleteById(Long id) {
        String sql = "delete from teacher_course where id = ?";
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1, id);
            psmt.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt);
        }
    }

    @Override
    public TeacherCourse selectById(Long id) {
        String sql = "select * from teacher_course where id = ?";
        TeacherCourse teacherCourse = new TeacherCourse();
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            psmt.setLong(1, id);
            rs = psmt.executeQuery();
            while (rs.next()) {
                teacherCourse.setId(rs.getLong(1));
                teacherCourse.setCourseId(rs.getLong(2));
                teacherCourse.setCourseId(rs.getLong(3));

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt, rs);
        }
        return teacherCourse;
    }

    @Override
    public List<TeacherCourse> selectList(TeacherCourse teacherCourse) {
        String sql = "select * from teacher_course where 1=1";
        List<TeacherCourse> list = new ArrayList<>();
        List<Object> param = new ArrayList<>();
        if (teacherCourse == null) {
            sql = "select * from lh_student_course";
        } else {
            if (teacherCourse.getId() != null) {
                sql += " and id = ?";
                param.add(teacherCourse.getId());
            }
            if (teacherCourse.getTeacherId() != null) {
                sql += " and student_id = ?";
                param.add(teacherCourse.getTeacherId());
            }
            if (teacherCourse.getCourseId() != null) {
                sql += " and course_id= ?";
                param.add(teacherCourse.getCourseId());
            }
        }
        try {
            con = JDBCUtils.getConnection();
            psmt = con.prepareStatement(sql);
            for (int i = 0; i < param.size(); i++) {
                psmt.setObject(i + 1, param.get(i));
            }
            rs = psmt.executeQuery();
            while (rs.next()) {
                TeacherCourse teacherCourse1 = new TeacherCourse();
                teacherCourse1.setId(rs.getLong(1));
                teacherCourse1.setTeacherId(rs.getLong(2));
                teacherCourse1.setCourseId(rs.getLong(3));
                list.add(teacherCourse1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResource(con, psmt, rs);
        }
        return list;
    }
}
